<?php
namespace app\admin\service;
class PhoneService
{
    //https://yunapp.baidu.com/console/bgs/doc/?d=bgs-phone&p=PHONE_API_server_token
    public $appkey = "91db6e69bf0346a9bd30eb6784c51c83";
    public $appSecret = "d0c7f2782a1fadf6b2b6177edec4919f";
    public $des_key = '4f2c3929d62b673542d5d9757d1b7ac7';
    public $errorMsg = '';
    public function parseCommonUrl($url,$nonce){
        // 拼接参数字符串
        $auth_ver = 3;

        $paramsStr = "appkey" . $this->appkey .
            "auth_ver" . $auth_ver .
            "nonce" . $nonce .
            $this->appSecret;

        // 使用 MD5 算法生成签名
        $sign = md5($paramsStr);

        $apiUrl = "{$url}?appkey={$this->appkey}&auth_ver={$auth_ver}&s={$sign}&nonce={$nonce}";
        return $apiUrl;

    }
    public function encrypt_with_base64($key, $iv, $message){
        $encrypted = openssl_encrypt($message, 'DES-EDE3-CBC', substr($key, 0, 24), 0, pack('J',$iv));
        return $encrypted;
    }
    public function serverToken($uuid,$instanceCode,$onlineTime){
        $param = [
            'uuid'=>$uuid,
            'instanceCodes'=>[$instanceCode],
            'onlineTime'=>$onlineTime,
            'grantControl'=>'CONTROL',
        ];
        $nonce =  time()*1000;
        $url = 'https://platform.armvm.com/auth/instance/cloud-phone-server-token';
        $apiUrl = $this->parseCommonUrl($url,$nonce);
        $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
        $pushData = [
            'msg'=>$msg,
            'createTime'=>$nonce,
        ];
        $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
        if($result['code'] != 0){
            $this->errorMsg = $result['msg'];
            return false;
        }

        $reData = $result['data'];
        if(isset($reData['data']['successList'][0])){
            return $reData['data']['successList'][0]['serverToken'];
        }else{
            $this->errorMsg = $reData['data']['errorList'][0]['errorMsg'] ?? '未知错误';
            return false;
        }
     }

     public function reboot($instanceCode){
         $param = [
             'padCodes'=>[$instanceCode],
         ];
         $nonce =  time()*1000;
         $url = 'https://platform.armvm.com/command/pad/reboot.html';
         $apiUrl = $this->parseCommonUrl($url,$nonce);
         $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
         $pushData = [
             'msg'=>$msg,
             'createTime'=>$nonce,
         ];
         $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
         if($result['code'] != 0){
             $this->errorMsg = $result['msg'];
             return false;
         }

         $reData = $result['data'];
         if(isset($reData['code']) && $reData['code'] == 0){
             return true;
         }else{
             $this->errorMsg = $reData['msg'] ?? '未知错误';
             return false;
         }
     }
     public function reset($instanceCode){
//        return 11;
         $param = [
             'padCodes'=>[$instanceCode],
         ];
         $nonce =  time()*1000;
         $url = 'https://platform.armvm.com/command/pad/reset.html';
         $apiUrl = $this->parseCommonUrl($url,$nonce);
         $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
         $pushData = [
             'msg'=>$msg,
             'createTime'=>$nonce,
         ];
         $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
         if($result['code'] != 0){
             $this->errorMsg = $result['msg'];
             return false;
         }

         $reData = $result['data'];
         if(isset($reData['code']) && $reData['code'] == 0){
             $task_id = $reData['data'][0]['taskId'] ?? 0;
             if($task_id){
                 db('cloudphone')->where('instance_code',$instanceCode)->update(['init_status'=>2,'task_id'=>$task_id]);
             }
             return $task_id;
         }else{
             $this->errorMsg = $reData['msg'] ?? '未知错误';
             return false;
         }
     }
     public function installapp($instanceCode,$appId){
         $param = [
             'appId'=>$appId,
             'instanceCodes'=>[$instanceCode]
         ];
         $nonce =  time()*1000;
         $url = 'https://platform.armvm.com/resources/instance/install-app';
         $apiUrl = $this->parseCommonUrl($url,$nonce);
         $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
         $pushData = [
             'msg'=>$msg,
             'createTime'=>$nonce,
         ];
         $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
         if($result['code'] != 0){
             $this->errorMsg = $result['msg'];
             return false;
         }

         $reData = $result['data'];
         if(isset($reData['code']) && $reData['code'] == 0){
             $taskId = $reData['data'][0]['taskId'] ?? 0;
             return $taskId;
         }else{
             $this->errorMsg = $reData['msg'] ?? '未知错误';
             return false;
         }
     }
    public function previweInstallapp($instanceCode,$appId){
        $param = [
            'appId'=>$appId,
            'instanceCodes'=>[$instanceCode]
        ];
        $nonce =  time()*1000;
        $url = 'https://platform.armvm.com/resources/app/app-builtin-install';
        $apiUrl = $this->parseCommonUrl($url,$nonce);
        $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
        $pushData = [
            'msg'=>$msg,
            'createTime'=>$nonce,
        ];
        $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
        if($result['code'] != 0){
            $this->errorMsg = $result['msg'];
            return false;
        }

        $reData = $result['data'];
        if(isset($reData['code']) && $reData['code'] == 0){
            $taskId = $reData['data'][0]['taskId'] ?? 0;
            return $taskId;
        }else{
            $this->errorMsg = $reData['msg'] ?? '未知错误';
            return false;
        }
    }
    public function backup($instanceCode,$snapshotName){
        $param = [
            'snapshotName'=>$snapshotName,
            'instanceCode'=>$instanceCode,
            'ossConfig'=>[
                'endpoint'=>'',
                'bucket'=>'',
                'accessKey'=>'',
                'secretKey'=>'',
                'protocol'=>'',
            ]
        ];
        $nonce =  time()*1000;
        $url = 'https://platform.armvm.com/resources/instance/backup';
        $apiUrl = $this->parseCommonUrl($url,$nonce);
        $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
        $pushData = [
            'msg'=>$msg,
            'createTime'=>$nonce,
        ];
        $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
        if($result['code'] != 0){
            $this->errorMsg = $result['msg'];
            return false;
        }

        $reData = $result['data'];
        if(isset($reData['code']) && $reData['code'] == 0){
            $task_id = $reData['data'][0]['taskId'] ?? 0;
            $snapshotId = $reData['data'][0]['snapshotId'] ?? 0;
            $snapshotName = $reData['data'][0]['snapshotName'] ?? 0;

            return true;
        }else{
            $this->errorMsg = $reData['msg'] ?? '未知错误';
            return false;
        }
    }
     public function restore($instanceCode,$snapshotId){
         $param = [
             'snapshotId'=>$snapshotId,
             'instanceCodes'=>[$instanceCode]
         ];
         $nonce =  time()*1000;
         $url = 'https://platform.armvm.com/resources/instance/restore';
         $apiUrl = $this->parseCommonUrl($url,$nonce);
         $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
         $pushData = [
             'msg'=>$msg,
             'createTime'=>$nonce,
         ];
         $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
         if($result['code'] != 0){
             $this->errorMsg = $result['msg'];
             return false;
         }

         $reData = $result['data'];
         if(isset($reData['code']) && $reData['code'] == 0){
             $task_id = $reData['data'][0]['taskId'] ?? 0;
             if($task_id){
                 db('cloudphone')->where('instance_code',$instanceCode)->update(['init_status'=>2,'task_id'=>$task_id]);
             }
             return true;
         }else{
             $this->errorMsg = $reData['msg'] ?? '未知错误';
             return false;
         }
     }

     public function taskInfo($instanceCode,$task_id){
         $param = [
             'taskIds'=>[$task_id]
         ];
         $nonce =  time()*1000;
         $url = 'https://platform.armvm.com/command/pad/execute-task-info.html';
         $apiUrl = $this->parseCommonUrl($url,$nonce);
         $msg = $this->encrypt_with_base64($this->des_key,$nonce,json_encode($param));
         $pushData = [
             'msg'=>$msg,
             'createTime'=>$nonce,
         ];
         $result = requestHttpCurl($apiUrl,json_encode($pushData),60);
         if($result['code'] != 0){
             $this->errorMsg = $result['msg'];
             return false;
         }

         $reData = $result['data'];
         if(isset($reData['code']) && $reData['code'] == 0){
             $task_id = $reData['data'][0]['taskId'] ?? 0;
             $taskStatus = $reData['data'][0]['taskStatus'] ?? 0;
             if($taskStatus == 7){
                 db('cloudphone')->where('instance_code',$instanceCode)->update(['init_status'=>1,'task_id'=>0]);
                 return true;
             }else if($taskStatus == 8){
                 db('cloudphone')->where('instance_code',$instanceCode)->update(['init_status'=>-1]);
                 $this->errorMsg = "执行失败";
                 return false;
             }else if($taskStatus == 6){
                 $this->errorMsg = "执行中";
                 return false;
             }else{
                 $this->errorMsg = "等待执行";
                 return false;
             }

         }else{
             $this->errorMsg = $reData['msg'] ?? '未知错误';
             return false;
         }
     }
}
